﻿package pluginfw.nio
{
    import flash.utils.*;

    public class Input extends Object
    {
        protected var buffer:ByteArray;
        protected var chars:ByteArray;

        public function Input(param1:ByteArray = null)
        {
            this.chars = new ByteArray();
            if (param1 != null)
            {
                this.buffer = param1;
                this.buffer.position = 0;
            }
            return;
        }// end function

        public function get bytesAvailable() : int
        {
            return this.buffer.bytesAvailable;
        }// end function

        public function setBytes(param1:ByteArray) : void
        {
            this.buffer = param1;
            this.buffer.position = 0;
            return;
        }// end function

        protected function require(param1:int) : int
        {
            if (this.buffer.bytesAvailable >= param1)
            {
                return this.buffer.bytesAvailable;
            }
            throw new Error();
        }// end function

        public function read() : int
        {
            if (this.buffer.bytesAvailable < 1)
            {
                return -1;
            }
            return this.buffer.readUnsignedByte() & 255;
        }// end function

        public function skip(param1:int) : void
        {
            this.buffer.position = this.buffer.position + param1;
            return;
        }// end function

        public function readByte() : int
        {
            this.require(1);
            return this.buffer.readByte();
        }// end function

        public function readByteUnsigned() : int
        {
            this.require(1);
            return this.buffer.readUnsignedByte();
        }// end function

        public function readBytes(param1:int) : ByteArray
        {
            var _loc_2:* = new ByteArray();
            this._readBytes(_loc_2, 0, param1);
            return _loc_2;
        }// end function

        protected function _readBytes(param1:ByteArray, param2:int, param3:int) : void
        {
            this.buffer.readBytes(param1, param2, param3);
            return;
        }// end function

        public function readInt() : int
        {
            this.require(4);
            return (this.buffer.readUnsignedByte() & 255) << 24 | (this.buffer.readUnsignedByte() & 255) << 16 | (this.buffer.readUnsignedByte() & 255) << 8 | this.buffer.readUnsignedByte() & 255;
        }// end function

        public function readVarInt(param1:Boolean) : int
        {
            if (this.require(1) < 5)
            {
                return this.readInt_slow(param1);
            }
            var _loc_2:* = this.buffer.readUnsignedByte();
            var _loc_3:* = _loc_2 & 127;
            if ((_loc_2 & 128) != 0)
            {
                _loc_2 = this.buffer.readUnsignedByte();
                _loc_3 = _loc_3 | (_loc_2 & 127) << 7;
                if ((_loc_2 & 128) != 0)
                {
                    _loc_2 = this.buffer.readUnsignedByte();
                    _loc_3 = _loc_3 | (_loc_2 & 127) << 14;
                    if ((_loc_2 & 128) != 0)
                    {
                        _loc_2 = this.buffer.readUnsignedByte();
                        _loc_3 = _loc_3 | (_loc_2 & 127) << 21;
                        if ((_loc_2 & 128) != 0)
                        {
                            _loc_2 = this.buffer.readUnsignedByte();
                            _loc_3 = _loc_3 | (_loc_2 & 127) << 28;
                        }
                    }
                }
            }
            return param1 ? (_loc_3) : (_loc_3 >>> 1 ^ -(_loc_3 & 1));
        }// end function

        private function readInt_slow(param1:Boolean) : int
        {
            var _loc_2:* = this.buffer.readUnsignedByte();
            var _loc_3:* = _loc_2 & 127;
            if ((_loc_2 & 128) != 0)
            {
                this.require(1);
                _loc_2 = this.buffer.readUnsignedByte();
                _loc_3 = _loc_3 | (_loc_2 & 127) << 7;
                if ((_loc_2 & 128) != 0)
                {
                    this.require(1);
                    _loc_2 = this.buffer.readUnsignedByte();
                    _loc_3 = _loc_3 | (_loc_2 & 127) << 14;
                    if ((_loc_2 & 128) != 0)
                    {
                        this.require(1);
                        _loc_2 = this.buffer.readUnsignedByte();
                        _loc_3 = _loc_3 | (_loc_2 & 127) << 21;
                        if ((_loc_2 & 128) != 0)
                        {
                            this.require(1);
                            _loc_2 = this.buffer.readUnsignedByte();
                            _loc_3 = _loc_3 | (_loc_2 & 127) << 28;
                        }
                    }
                }
            }
            return param1 ? (_loc_3) : (_loc_3 >>> 1 ^ -(_loc_3 & 1));
        }// end function

        public function readString() : String
        {
            var _loc_1:* = this.require(1);
            var _loc_2:* = this.buffer.readUnsignedByte();
            if ((_loc_2 & 128) == 0)
            {
                return this.readAscii();
            }
            var _loc_3:* = _loc_1 >= 5 ? (this.readUtf8Length(_loc_2)) : (this.readUtf8Length_slow(_loc_2));
            switch(_loc_3)
            {
                case 0:
                {
                    return null;
                }
                case 1:
                {
                    return "";
                }
                default:
                {
                    break;
                }
            }
            _loc_3 = _loc_3 - 1;
            this.chars.clear();
            this.readUtf8(_loc_3);
            var _loc_4:* = this.chars.position;
            this.chars.position = 0;
            return this.chars.readUTFBytes(_loc_4);
        }// end function

        private function readUtf8Length(param1:int) : int
        {
            var _loc_2:* = param1 & 63;//_loc_2 = param1-64  _loc_2保存长度
            if ((param1 & 64) != 0)//param1>=64
            {
                param1 = this.buffer.readUnsignedByte();//再读一个字节进来
                _loc_2 = _loc_2 | (param1 & 127) << 6;//现长
                if ((param1 & 128) != 0)//param1>=128
                {
                    param1 = this.buffer.readUnsignedByte();
                    _loc_2 = _loc_2 | (param1 & 127) << 13;
                    if ((param1 & 128) != 0)
                    {
                        param1 = this.buffer.readUnsignedByte();
                        _loc_2 = _loc_2 | (param1 & 127) << 20;
                        if ((param1 & 128) != 0)
                        {
                            param1 = this.buffer.readUnsignedByte();
                            _loc_2 = _loc_2 | (param1 & 127) << 27;
                        }
                    }
                }
            }
            return _loc_2;
        }// end function

        private function readUtf8Length_slow(param1:int) : int
        {
            var _loc_2:* = param1 & 63;
            if ((param1 & 64) != 0)
            {
                this.require(1);
                param1 = this.buffer.readUnsignedByte();
                _loc_2 = _loc_2 | (param1 & 127) << 6;
                if ((param1 & 128) != 0)
                {
                    this.require(1);
                    param1 = this.buffer.readUnsignedByte();
                    _loc_2 = _loc_2 | (param1 & 127) << 13;
                    if ((param1 & 128) != 0)
                    {
                        this.require(1);
                        param1 = this.buffer.readUnsignedByte();
                        _loc_2 = _loc_2 | (param1 & 127) << 20;
                        if ((param1 & 128) != 0)
                        {
                            this.require(1);
                            param1 = this.buffer.readUnsignedByte();
                            _loc_2 = _loc_2 | (param1 & 127) << 27;
                        }
                    }
                }
            }
            return _loc_2;
        }// end function

        private function readUtf8(param1:int) : void
        {
            var _loc_4:int = 0;
            var _loc_2:int = 0;
            var _loc_3:* = Math.min(this.require(1), param1);
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = this.buffer.readByte();
                if (_loc_4 < 0)
                {
                    var _loc_5:* = this.buffer;
                    var _loc_6:* = this.buffer.position - 1;
                    _loc_5.position = _loc_6;
                    break;
                }
                _loc_2 = _loc_2 + 1;
                this.chars.writeByte(_loc_4);
            }
            if (_loc_2 < param1)
            {
                this.readUtf8_slow(param1, _loc_2);
            }
            return;
        }// end function

        private function readUtf8_slow(param1:int, param2:int) : void
        {
            var _loc_5:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = this.buffer.position;
            while (param2 < param1)
            {
                
                _loc_5 = this.buffer.readByte() & 255;
                switch(_loc_5 >> 4)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    {
                        this.chars.writeByte(_loc_5);
                        break;
                    }
                    case 12:
                    case 13:
                    {
                        this.chars.writeByte(_loc_5);
                        this.chars.writeByte(this.buffer.readByte());
                        break;
                    }
                    case 14:
                    {
                        this.chars.writeByte(_loc_5);
                        this.chars.writeByte(this.buffer.readByte());
                        this.chars.writeByte(this.buffer.readByte());
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                param2 = param2 + 1;
            }
            return;
        }// end function

        private function readAscii() : String
        {
            var _loc_5:int = 0;
            var _loc_1:* = this.buffer.position;
            var _loc_2:* = this.buffer.position;
            var _loc_3:* = _loc_2 - 1;
            var _loc_4:* = this.buffer.length;
            do
            {
                
                if (_loc_2 == _loc_4)
                {
                    this.buffer.position = _loc_1 - 1;
                    return this.readAscii_slow();
                }
                _loc_5 = this.buffer.readUnsignedByte();
                _loc_2 = _loc_2 + 1;
            }while ((_loc_5 & 128) == 0)
            this.buffer[(_loc_2 - 1)] = this.buffer[(_loc_2 - 1)] & 127;
            this.buffer.position = _loc_1 - 1;
            var _loc_6:* = (this.buffer as ByteArray).readMultiByte(_loc_2 - _loc_3, "iso-8859-1");
            this.buffer[(_loc_2 - 1)] = this.buffer[(_loc_2 - 1)] | 128;
            return _loc_6;
        }// end function

        private function readAscii_slow() : String
        {
            throw new Error("数据流中有不支持的字符串解析");
        }// end function

        public function readFloat() : Number
        {
            return this.buffer.readFloat();
        }// end function

        public function readFloat2(param1:Number, param2:Boolean) : Number
        {
            return Number(this.readInt_slow(param2)) / param1;
        }// end function

        public function readShort() : int
        {
            return this.buffer.readShort();
        }// end function

        public function readShortUnsigned() : int
        {
            return this.buffer.readUnsignedShort();
        }// end function

        public function readLong() : Long
        {
            var _loc_1:* = this.buffer.readUnsignedByte() << 24;
            _loc_1 = _loc_1 | this.buffer.readUnsignedByte() << 16;
            _loc_1 = _loc_1 | this.buffer.readUnsignedByte() << 8;
            _loc_1 = _loc_1 | this.buffer.readUnsignedByte();
            var _loc_2:* = this.buffer.readUnsignedByte() << 24;
            _loc_2 = _loc_2 | this.buffer.readUnsignedByte() << 16;
            _loc_2 = _loc_2 | this.buffer.readUnsignedByte() << 8;
            _loc_2 = _loc_2 | this.buffer.readUnsignedByte();
            return LongUtils.create(_loc_1, _loc_2);
        }// end function

        public function readVarLong(param1:Boolean) : Long
        {
            var _loc_6:int = 0;
            var _loc_2:* = this.buffer.readUnsignedByte();
            var _loc_3:* = _loc_2 & 127;
            var _loc_4:uint = 0;
            if ((_loc_2 & 128) != 0)
            {
                _loc_2 = this.buffer.readUnsignedByte();
                _loc_3 = _loc_3 | (_loc_2 & 127) << 7;
                if ((_loc_2 & 128) != 0)
                {
                    _loc_2 = this.buffer.readUnsignedByte();
                    _loc_3 = _loc_3 | (_loc_2 & 127) << 14;
                    if ((_loc_2 & 128) != 0)
                    {
                        _loc_2 = this.buffer.readUnsignedByte();
                        _loc_3 = _loc_3 | (_loc_2 & 127) << 21;
                        if ((_loc_2 & 128) != 0)
                        {
                            _loc_2 = this.buffer.readUnsignedByte();
                            _loc_6 = _loc_2 & 127;
                            _loc_4 = 0 << _loc_6 | _loc_6 >>> 4 | _loc_4;
                            _loc_3 = _loc_3 | _loc_6 << 28;
                            if ((_loc_2 & 128) != 0)
                            {
                                _loc_2 = this.buffer.readUnsignedByte();
                                _loc_4 = _loc_4 | (_loc_2 & 127) << 3;
                                if ((_loc_2 & 128) != 0)
                                {
                                    _loc_2 = this.buffer.readUnsignedByte();
                                    _loc_4 = _loc_4 | (_loc_2 & 127) << 10;
                                    if ((_loc_2 & 128) != 0)
                                    {
                                        _loc_2 = this.buffer.readUnsignedByte();
                                        _loc_4 = _loc_4 | (_loc_2 & 127) << 17;
                                        if ((_loc_2 & 128) != 0)
                                        {
                                            _loc_2 = this.buffer.readByte();
                                            _loc_4 = _loc_4 | _loc_2 << 24;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            var _loc_5:* = LongUtils.create(_loc_4, _loc_3);
            if (!param1)
            {
                _loc_5.optimizePositive();
            }
            return _loc_5;
        }// end function

        public function readBoolean() : Boolean
        {
            return this.buffer.readUnsignedByte() == 1;
        }// end function

        public function readChar() : int
        {
            return this.buffer.readUnsignedShort();
        }// end function

        public function readDouble() : Number
        {
            return this.buffer.readDouble();
        }// end function

        public function readVarInts(param1:int, param2:Boolean) : Vector.<int>
        {
            var _loc_3:* = new Vector.<int>;
            var _loc_4:int = 0;
            while (_loc_4 < param1)
            {
                
                _loc_3[_loc_4] = this.readVarInt(param2);
                _loc_4 = _loc_4 + 1;
            }
            return _loc_3;
        }// end function

        public function readVarLongs(param1:int, param2:Boolean) : Vector.<Long>
        {
            var _loc_3:* = new Vector.<Long>;
            var _loc_4:int = 0;
            while (_loc_4 < param1)
            {
                
                _loc_3[_loc_4] = this.readVarLong(param2);
                _loc_4 = _loc_4 + 1;
            }
            return _loc_3;
        }// end function

        public function readInts(param1:int) : Vector.<int>
        {
            var _loc_2:* = new Vector.<int>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readInt();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

        public function readLongs(param1:int) : Vector.<Long>
        {
            var _loc_2:* = new Vector.<Long>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readLong();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

        public function readFloats(param1:int) : Vector.<Number>
        {
            var _loc_2:* = new Vector.<Number>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readFloat();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

        public function readShorts(param1:int) : Vector.<int>
        {
            var _loc_2:* = new Vector.<int>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readShort();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

        public function readChars(param1:int) : Vector.<int>
        {
            var _loc_2:* = new Vector.<int>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readChar();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

        public function readDoubles(param1:int) : Vector.<Number>
        {
            var _loc_2:* = new Vector.<Number>;
            var _loc_3:int = 0;
            while (_loc_3 < param1)
            {
                
                _loc_2[_loc_3] = this.readDouble();
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }// end function

    }
}
